package com.example.myview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Random;

public class MyKLineView extends View {
    private Paint mPaint;
    private Paint mPaint1;
    private Paint mMovingPaint;
    private static final int HANDLER_WHAT = 100;
    private static final int RATE = 10; // 移动速率
    private int mR = 0;  // 移动速率
    private int mRate = RATE;   // 速率
    private static final int FRAME = 1000;  // 1000帧
    private PointF start,end,control;
    private Path path = new Path();      //控制点集合
    private ArrayList<PointF> mControlPoints = null;
    private ArrayList<PointF> mBezierPoints = null; // 贝塞尔曲线点集
    private PointF mBezierPoint = null; // 贝塞尔曲线移动点
    private Path mBezierPath = null; //贝赛尔曲线路径
    private Paint mBezierPaint = null; //贝赛尔曲线画笔

    private ArrayList<PointF> mAllPoints = null;  //所有点

    private int count;

    private int size;

    private int con;

    private int fontX;







    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == HANDLER_WHAT) {
                mR += mRate;
                if (mR >= mBezierPoints.size()) {
                    removeMessages(HANDLER_WHAT);
                    mR = 0;
                    return;
                }
                if (mR != mBezierPoints.size() - 1 && mR + mRate >= mBezierPoints.size()) {
                    mR = mBezierPoints.size() - 1;                 }
                 //Bezier点
                mBezierPoint = new PointF(mBezierPoints.get(mR).x, mBezierPoints.get(mR).y);
                // 切线点
                if (mR == 1000){
                    count++;
                    System.out.printf("画了%d条线段",count);
                    mR=0;
                    if(count!=size){
                        mControlPoints.remove(0);
                        mControlPoints.add(mAllPoints.get(count));
                        mBezierPoints.clear();
                        mBezierPoints = buildBezierPoints();
                    }else {
                        return;
                    }

                }
                invalidate();
                }
        }
    };

          


    public MyKLineView(Context context) {
        super(context);
        init();
    }

    public MyKLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyKLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }



    private void  init() {

        mMovingPaint = new Paint();
        mMovingPaint.setColor(Color.BLACK);
        mMovingPaint.setAntiAlias(true);
        mMovingPaint.setStyle(Paint.Style.FILL);




        mBezierPath = new Path();

        mBezierPaint = new Paint();
        mBezierPaint.setColor(Color.RED);
        mBezierPaint.setStrokeWidth(10);
        mBezierPaint.setStyle(Paint.Style.STROKE);
        mBezierPaint.setAntiAlias(true);

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.GRAY);
        mPaint.setStrokeWidth(10);
        mPaint.setAntiAlias(true);

        mPaint1 = new Paint();
        mPaint1.setStyle(Paint.Style.FILL);
        mPaint1.setColor(Color.BLUE);
        mPaint1.setStrokeWidth(7);







//        start = new PointF(200, 200);
//        end = new PointF(700, 1500);
        mAllPoints = new ArrayList<>();
        PointF  pointF = new PointF();
//        for(int j = 0;j<10;j++){
//            Random r =new Random();
//            mAllPoints.add(pointF);
//        }

        mAllPoints.add(new PointF(0,700));
        mAllPoints.add(new PointF(100,800));
        mAllPoints.add(new PointF(200,500));
        mAllPoints.add(new PointF(300,600));
        mAllPoints.add(new PointF(400,600));
        mAllPoints.add(new PointF(500,900));
        mAllPoints.add(new PointF(600,400));
        mAllPoints.add(new PointF(700,300));
        mAllPoints.add(new PointF(800,900));
        mAllPoints.add(new PointF(900,100));
        mAllPoints.add(new PointF(1000,500));
        mAllPoints.add(new PointF(1100,400));
        mAllPoints.add(new PointF(1200,600));
        mAllPoints.add(new PointF(1300,500));
        mControlPoints = new ArrayList<>();
        mControlPoints.add(mAllPoints.get(0));
        mControlPoints.add(mAllPoints.get(1));
        mBezierPoints = buildBezierPoints();

        count = 1;
        con =  0;
        fontX =  700;
        size = mAllPoints.size();

    }
    private float deCasteljauX(int i, int j, float t) {
        if (i == 1) {
            return (1 - t) * mControlPoints.get(j).x + t * mControlPoints.get(j + 1).x;
        }
        return (1 - t) * deCasteljauX(i - 1, j, t) + t * deCasteljauX(i - 1, j + 1, t);
    }

    private float deCasteljauY(int i, int j, float t) {
        if (i == 1) {
            return (1 - t) * mControlPoints.get(j).y + t * mControlPoints.get(j + 1).y;
        }
        return (1 - t) * deCasteljauY(i - 1, j, t) + t * deCasteljauY(i - 1, j + 1, t);
    }

    //创建贝塞曲线集合点
    private ArrayList<PointF> buildBezierPoints() {
        ArrayList<PointF> points = new ArrayList<>();
        int order = mControlPoints.size() - 1;
        float delta = 1.0f / FRAME;
        for (float t = 0; t <= 1; t += delta) {
            // Bezier点集
            points.add(new PointF(deCasteljauX(order, 0, t), deCasteljauY(order, 0, t)));
        }
        return points;
    }
    @Override
    protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);
         canvas.drawPoint(mControlPoints.get(0).x,mControlPoints.get(0).y,mPaint);
         canvas.drawPoint(mControlPoints.get(1).x,mControlPoints.get(1).y,mPaint);
//         canvas.drawPoint(end.x,end.y,mPaint);
        // 绘制辅助线
//         mPaint.setStrokeWidth(4);
//         int size = mControlPoints.size();
//         PointF pointF;
//         for (int i=0;i<size;i++){
//            pointF = mControlPoints.get(i);
//            if(i>0){
//                canvas.drawLine(mControlPoints.get(i-1).x,mControlPoints.get(i-1).y,pointF.x,pointF.y,mPaint);
//            }
//        }
        //控制画布移动
        if (mBezierPoint == null) {
            mPaint.reset();
            mBezierPoint = mBezierPoints.get(0);
            mBezierPath.moveTo(mBezierPoint.x, mBezierPoint.y);
        }

        canvas.drawLine(fontX,0,fontX,getHeight(),mPaint1);
        if(mBezierPoint.x>=(fontX+(con*400))){
            con++;
            canvas.save();
            canvas.translate(-400,0);
            Log.i("c1" ,"onDraw: "+mBezierPoint.x+"con: "+(fontX+(con*400)));

        }//绘制Bezier曲线
        mBezierPath.lineTo(mBezierPoint.x, mBezierPoint.y);
        canvas.drawPath(mBezierPath,mBezierPaint);
        canvas.drawCircle(mBezierPoint.x,mBezierPoint.y,12,mMovingPaint);
        canvas.drawLine(0,mBezierPoint.y,getWidth(),mBezierPoint.y,mPaint1);
        mHandler.removeMessages(HANDLER_WHAT);
        mHandler.sendEmptyMessage(HANDLER_WHAT);
        //绘制开始地点



    }


}
